//
//  ShopViewController.m
//  YIChuSeller
//
//  Created by xsj on 2018/11/22.
//  Copyright © 2018年 xiangshengjun. All rights reserved.
//

#import "GXHomeViewController.h"
#import "ShopNewCell.h"
#import "ShopManagerCell.h"
#import "GXShopHeaderView.h"
#import "GXSettViewController.h"
#import "GXChangePhoneController.h"
#import "GXNewsWarnController.h"
#import "GXOrderNewsViewController.h"
#import "GXShopAuthenManagerController.h"
#import "GXShopAuthenManagerController.h"
#import "GXContentManagerController.h"
#import "GXTableNumberViewController.h"
#import "GXShopIntroduceController.h"
#import "GXNewsCenterViewController.h"
#import "GXAdviceFeedBackController.h"
#import "GXShopAuthenController.h"
#import "NewsAndAdviceManager.h"
@interface GXHomeViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong) UITableView * homeTableView;
@property(nonatomic,strong) GXShopHeaderView *shopView;
@property(nonatomic,strong) UIButton * statuBtn;
@end
static NSString *const shopNewCell=@"ShopNewCell";
static NSString *const shopManagerCell=@"ShopManagerCell";
@implementation GXHomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor clearColor];
    [self initTotalView];
    [self requestShopManageDetailMessage];
}
#pragma 添加界面
- (void)initTotalView
{
    [self.view addSubview:self.homeTableView];
    self.homeTableView.tableHeaderView = self.shopView;
}
- (UITableView *)homeTableView
{
    if (_homeTableView == nil) {
        _homeTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,-Height_StatusBar,kUIScreenWidth,kUIScreenHeight) style:UITableViewStyleGrouped];
        _homeTableView.delegate=self;
        _homeTableView.dataSource=self;
        _homeTableView.sectionFooterHeight =0.01f;
        _homeTableView.sectionHeaderHeight = 20;
        _homeTableView.showsHorizontalScrollIndicator=NO;
        _homeTableView.showsVerticalScrollIndicator=NO;
        _homeTableView.separatorStyle=UITableViewCellSeparatorStyleNone;
        _homeTableView.backgroundColor = kGlobalbgColor;
        [_homeTableView  registerNib:[UINib nibWithNibName:@"ShopNewCell" bundle:nil] forCellReuseIdentifier:shopNewCell];
        [_homeTableView  registerNib:[UINib nibWithNibName:@"ShopManagerCell" bundle:nil] forCellReuseIdentifier:shopManagerCell];
    }
    return _homeTableView;
}
- (GXShopHeaderView *)shopView
{
    if (_shopView == nil) {
        _shopView =[GXShopHeaderView headView];
        _shopView.height = 0.35* kUIScreenHeight;
        _shopView.accountModel = [AccountTool sharedAccountTool].account;
    }
    return _shopView;
}

  
#pragma 请求店铺管理的数据
- (void)requestShopManageDetailMessage
{
    NSString * storeId = [AccountTool sharedAccountTool].account.sotres[@"storeId"];
    [ShopAuthenticationManager selectShopAllMessageWithStoreID:storeId success:^(id responseObject) {
        if (responseObject) {
            [[NSUserDefaults standardUserDefaults]setObject:responseObject[@"result"] forKey:@"ShopDetail"];
            [[NSUserDefaults standardUserDefaults]synchronize];
        }
    } fail:^(id responseObject) {
        
    }];
}


#pragma mark ---UITableViewDataSource UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==0) {
        ShopManagerCell *Cell = [tableView dequeueReusableCellWithIdentifier:shopManagerCell];
          Cell.selectionStyle = UITableViewCellSelectionStyleNone;
        Cell.cellManagerItemBlock = ^(UIButton * itemButton) {
            switch (itemButton.tag) {
            case 11:{
                    //认证
                NSString *userState = [AccountTool sharedAccountTool].account.users[@"state"];
                if ([userState isEqualToString:@"1"]) {
                    [SVProgressHUD showImage:nil status:@"用户已认证"];
                    return ;
                }
                   [self jumpController:[GXShopAuthenController class]];
                }
                     break;
            case 12:{
                    [self jumpController:[GXShopAuthenManagerController class]];
                    //店铺管理
                }
                    break;
            case 13:{
                    [self jumpController:[GXContentManagerController class]];
                    //内容管理
                }
                break;
            case 14:{
                  //营收统计
                [SVProgressHUD showImage:nil status:@"暂未开发"];
                }
                
                break;
            case 15:{
                    [self jumpController:[GXTableNumberViewController class]];
                    //桌号
                }
                break;
                
            default:
                break;
            }
        };
        return Cell;
    }
    ShopNewCell *Cell =[tableView dequeueReusableCellWithIdentifier:shopNewCell];
    Cell.selectionStyle = UITableViewCellSelectionStyleNone;
    Cell.cellItemBlock =^(UIButton * button)
    {
        switch (button.tag) {
            case 21:{
                [self jumpController:[GXSettViewController class]];
                //设置
                break;
            }
            case 22:{
                [self jumpController:[GXNewsCenterViewController class]];
                //消息
            }
                break;
            case 23:
            {
                WeakSelf(weakSelf);
                UIAlertController * alertController = [UIAlertController alertControllerWithTitle:nil message:@"客服热线:0731-84433847" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction * serviceAction = [UIAlertAction actionWithTitle:@"联系客服" style:UIAlertActionStyleDestructive handler:^(UIAlertAction * _Nonnull action) {
                    NSMutableString * str=[[NSMutableString alloc] initWithFormat:@"telprompt://%@",@"0731-84433847"];
                    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:str]];
                }];
                UIAlertAction *cancleAction = [UIAlertAction actionWithTitle:@"取消" style: UIAlertActionStyleCancel  handler:^(UIAlertAction * _Nonnull action){
                    [weakSelf dismissViewControllerAnimated:YES completion:nil];
                }];
                [alertController addAction:serviceAction];
                [alertController addAction:cancleAction];
                [weakSelf presentViewController:alertController animated:YES completion:nil];
            }
                break;
            case 24:{
                [self jumpController:[GXAdviceFeedBackController class]];
                //意见反馈
            }
                
                break;
    
                
            default:
                break;
        }
    };
    return Cell;
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 140;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 0.01f;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView * headerView = [[UIView alloc]init];
    headerView.backgroundColor = [UIColor clearColor];
    return headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView * footerView = [[UIView alloc]init];
    footerView.backgroundColor = [UIColor clearColor];
    return footerView;
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}
-(void)jumpController:(Class)VC
{
    UIViewController * controller = [[VC alloc]init];
    [self.navigationController pushViewController:controller animated:YES];
}
@end
